<?php
namespace app\member\controller;
class Member extends Common{
    function index(){
        $user=$this->user;

        $devs=model('device')->where(['member_id'=>$user['id']])->count();
        $softs=model('software')->where(['member_id'=>$user['id']])->count();
        $socials=model('social')->where(['member_id'=>$user['id']])->count();
        $this->assign('devs',$devs);
        $this->assign('softs',$softs);
        $this->assign('socials',$socials);

        // 任务菜单
        $taskgroup=model('taskgroup')->readAll(['status'=>1,'pc'=>1],0,['with'=>'taskoptionspc']);
        $this->assign('taskgroup',$taskgroup);

        $options=[
            'field'=>'id,name',
            'withCount'=>'device',
        ];
        $dev_group=model('group')->readAll(['member_id'=>$user['id'],'type'=>0],0,$options);
        $this->assign('dev_group',$dev_group);
        $this->assign('tasknav',config('task.'));
        // 素材分组
        $cate=controller('category')->getCates();
        $this->assign('cate',json_encode($cate));
        // 默认素材组ID
        $cate_id=isset($cate[0])?$cate[0]['id']:0;
        $this->assign('cate_id',$cate_id);
        // 默认显示素材组
        $source=controller('source')->getSources($cate_id,'',['uid'=>$user['id']]);
        $this->assign('source',$source);
        
        return view();
    }
    function changePwd(){
        if(request()->isPost()){
            $data=[
                'oldPwd'=>input('oldPwd/s',''),
                'pwd'=>input('pwd/s',''),
                'rpwd'=>input('rpwd/s',''),
            ];
            $validate=$this->validate($data,[
                'oldPwd|'.lang('Old password')=>'require',
                'pwd|'.lang('New password')=>'require',
                'rpwd|'.lang('Duplicate password')=>'require|confirm:pwd',
            ]);
            if(true!==$validate){
                return json($validate,1);
            }
            $user=$this->user;
            $user=model($this->table)->read($user['id']);
            if($user['login_pwd']!==encrypt_pwd($data['oldPwd'])){
                return json(lang('Incorrect username or password'),1);
            }
            $edit=model($this->table)->edit(['id'=>$user['id'],'login_pwd'=>encrypt_pwd($data['pwd'])]);
            if(empty($edit)){
                return json(lang('Update faliure'),1);
            }
            return json([
                'msg'=>lang('Update success'),
                'url'=>url(''),
            ]);
        }
        return view();
    }
    
}
